package com.neusoft.bizcore.activiti.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.activiti.components.ActUserAuthorityConditionComponent;
import com.neusoft.bizcore.activiti.dto.ProcessDTO;
import com.neusoft.bizcore.activiti.service.ProcessService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "业务工作流定义管理")
@RestController
@RequestMapping(value = "/api/w/act/process")
public class ActProcessController {

    @Autowired
    private ProcessService processService;
    @Autowired
    private ActUserAuthorityConditionComponent userAuthorityConditionComponent;

    @ApiOperation("查询所有业务流程")
    @GetMapping
    public ResultListDTO<ProcessDTO> index(final Searchable searchable, final UserBean user) {
        this.userAuthorityConditionComponent.addUserRolesToSearchCondition(searchable, user);
        final List<ProcessDTO> DTOs = this.processService.index(searchable);
        final ResultListDTO<ProcessDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有业务流程")
    @GetMapping("/p")
    public PageResultDTO<ProcessDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.processService.search(pageable, searchable));
    }

    @ApiOperation("创建业务流程")
    @PostMapping
    public ResultDTO<ProcessDTO> create(@RequestBody final ProcessDTO dto) {
        final ProcessDTO result = this.processService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("业务流程详情")
    @GetMapping("/{id}")
    public ResultDTO<ProcessDTO> details(@PathVariable final Long id) {
        final ProcessDTO dto = this.processService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("业务流程详情")
    @GetMapping("router/{code}")
    public ResultDTO<ProcessDTO> details(@PathVariable final String code) {
        final ProcessDTO dto = this.processService.details(code);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除业务流程")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.processService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("修改业务流程")
    @PutMapping("/{id}")
    public ResultDTO<ProcessDTO> update(@PathVariable final Long id,
            @RequestBody final ProcessDTO dto) {
        final ProcessDTO result = this.processService.update(id, dto);
        return ResultDTO.success(result);
    }
}
